/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.global.adapter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.ToolContext;
import org.apache.velocity.tools.ToolManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import cn.com.ksplatform.modules.autocode.dto.ColumnArray;
import cn.com.ksplatform.modules.autocode.dto.Propertys;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;
import cn.com.ksplatform.modules.autocode.manager.service.SenceService;
import cn.com.ksplatform.modules.autocode.util.SenceUtil;
import cn.com.ksplatform.modules.autocode.util.ToolUtil;

/**
 * ClassName:Adapter_1.java <br/>
 * Date: 2015年12月30日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Component
public class Adapter_1 implements IAdapter{
	@Autowired
	@Qualifier("modules.autocode.SenceService")
	SenceService senceService;
	
	@Override
	public void doAdapter(VelocityConfigurer config,  Sence senceModel, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		VelocityEngine ve = config.getVelocityEngine();
		
		Map param = new HashMap<String, Object>();
		param.put("request", request);
		param.putAll(ToolUtil.gettools());
		
		Propertys pts = SenceUtil.getPropertys(senceModel);
		param.put("propertys", pts.getPropertys());
		
		ColumnArray mainColumns = SenceUtil.getColumns(senceModel, "MainTable.xml");
		
		//获取子表集合
		Object[] childrens = senceService.getChildrenCloumnsBySence(senceModel, "children");
		//子表集合
		
		//ColumnArray childrenColumns = SenceUtil.getColumns(senceModel, "ChildrenTable.xml");
		
		Map mainMap = SenceUtil.ClassifyColumns(mainColumns);
		//Map childrenMap = SenceUtil.ClassifyColumns(childrenColumns);
		
		param.put("main", mainMap);
		//param.put("children", childrenMap);
		param.put("sence", senceModel);
		param.put("mainColumns", mainColumns);
		param.put("childrens", childrens);
		
		
		//ve.mergeTemplate("cn/com/ksplatform/modules/autocode/template/page/zzb.vm", context, response.getWriter());
		
		String html = VelocityEngineUtils.mergeTemplateIntoString(ve,"cn/com/ksplatform/modules/autocode/template/page/zzb.vm","UTF-8",param);
		response.getWriter().write(html);
	}
	
	

}
